package com.app.voice;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

import com.app.util.Utils_Log;

import java.util.List;

/**
 * Created by matengfei on 15/8/6.
 */
public class MySensor implements SensorEventListener{

    private final String TAG = MySensor.class.getSimpleName();

    private static final float TOO_DARK_LUX = 45.0f;
    private static final float BRIGHT_ENOUGH_LUX = 450.0f;

    private SensorManager manager;
    private Sensor light;



    public void init(Context context){
        manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
        light = manager.getDefaultSensor(Sensor.TYPE_LIGHT);

    }

    public void printSupportSensor(){
        List<Sensor> listdata = manager.getSensorList(Sensor.TYPE_ALL);
        for(Sensor item : listdata){
            Utils_Log.i(TAG, item.toString());
        }

    }



    public void OnResume() {
        manager.registerListener(this, light, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void OnPause() {
        manager.unregisterListener(this);
    }


    // 值发送变化将调用
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        float ambientLightLux = sensorEvent.values[0];
        if (ambientLightLux <= TOO_DARK_LUX) {
            //cameraManager.setTorch(true);
        } else if (ambientLightLux >= BRIGHT_ENOUGH_LUX) {
            //cameraManager.setTorch(false);
        }

    }

    // 精度有变化将被调用
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }


}
